/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumjdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Enumeration;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives related to the <tt>java.sql.DriverManager</tt> class.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
@PrimitiveProvider
public final class DriverManagers {

    /**
     * No instance of this class.
     */
    private DriverManagers() {
    } // end empty constructor

    /**
     * Deregisters the passed driver.
     * @param ctxt context
     * @param driver driver
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_drivermanager_deregister_driver(final CodeRunner ctxt,
                                                                    final Value driver)
    throws Fail.Exception {
        try {
            final Driver d = (Driver) driver.asBlock().asCustom();
            DriverManager.deregisterDriver(d);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_drivermanager_deregister_driver(CodeRunner, Value)'

    /**
     * Connects to a database.
     * @param ctxt context
     * @param url url
     * @return a new connection
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_drivermanager_get_connection(final CodeRunner ctxt,
                                                                 final Value url)
        throws Fail.Exception {
        try {
            final Connection c =
                DriverManager.getConnection(url.asBlock().asString());
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_drivermanager_get_connection(CodeRunner, Value)'

    /**
     * Connects to a database.
     * @param ctxt context
     * @param url url
     * @param props properties used for connection
     * @return a new connection
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_drivermanager_get_connection_with_properties(final CodeRunner ctxt,
                                                                                 final Value url,
                                                                                 final Value props)
        throws Fail.Exception {
        try {
            final Connection c =
                DriverManager.getConnection(url.asBlock().asString(),
                                            Misc.decodeProperties(props));
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_drivermanager_get_connection_with_properties(CodeRunner, Value, Value)'

    /**
     * Connects to a database.
     * @param ctxt context
     * @param url url
     * @param user user name
     * @param passwd password
     * @return a new connection
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_drivermanager_get_connection_with_user_passwd(final CodeRunner ctxt,
                                                                                  final Value url,
                                                                                  final Value user,
                                                                                  final Value passwd)
        throws Fail.Exception {
        try {
            final Connection c =
                DriverManager.getConnection(url.asBlock().asString(),
                                            user.asBlock().asString(),
                                            passwd.asBlock().asString());
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_drivermanager_get_connection_with_user_passwd(CodeRunner, Value, Value, Value)'

    /**
     * Returns a driver that understands the passed URL.
     * @param ctxt context
     * @param url url
     * @return a driver that understands the passed URL
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_drivermanager_get_driver(final CodeRunner ctxt,
                                                             final Value url)
        throws Fail.Exception {
        try {
            final Driver d = DriverManager.getDriver(url.asBlock().asString());
            return Cadmium.createObject(d);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_drivermanager_get_driver(CodeRunner, Value)'

    /**
     * Returns the list of all loaded drivers.
     * @param ctxt context
     * @param unit ignored
     * @return the list of all loaded drivers
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_drivermanager_get_drivers(final CodeRunner ctxt,
                                                              final Value unit)
        throws Fail.Exception {
        try {
            final Enumeration<Driver> l = DriverManager.getDrivers();
            Value res = Value.EMPTY_LIST;
            while (l.hasMoreElements()) {
                final Driver d = l.nextElement();
                final Value v = Cadmium.createObject(d);
                final Block cons = Block.createBlock(Block.TAG_CONS, v, res);
                res = Value.createFromBlock(cons);
            } // end while
            return res;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_drivermanager_get_drivers(CodeRunner, Value)'

    /**
     * Returns the maximum timeout in seconds.
     * @param ctxt context
     * @param unit ignored
     * @return the maximum timeout in seconds
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_drivermanager_get_login_timeout(final CodeRunner ctxt,
                                                                    final Value unit)
        throws Fail.Exception {
        try {
            final Block res = Block.createCustom(Custom.INT_32_SIZE,
                                                 Custom.INT_32_OPS);
            res.setInt32(DriverManager.getLoginTimeout());
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_drivermanager_get_login_timeout(CodeRunner, Value)'

    /**
     * Prints a message to the JDBC log stream.
     * @param ctxt context
     * @param message message
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_drivermanager_print_log(final CodeRunner ctxt,
                                                            final Value message)
        throws Fail.Exception {
        try {
            DriverManager.println(message.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_drivermanager_print_log(CodeRunner, Value)'

    /**
     * Registers the passed driver.
     * @param ctxt context
     * @param driver driver
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_drivermanager_register_driver(final CodeRunner ctxt,
                                                                  final Value driver)
        throws Fail.Exception {
        try {
            final Driver d = (Driver) driver.asBlock().asCustom();
            DriverManager.registerDriver(d);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_drivermanager_register_driver(CodeRunner, Value)'

    /**
     * Changes the maximum timeout.
     * @param ctxt context
     * @param seconds new timeout value
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_drivermanager_set_login_timeout(final CodeRunner ctxt,
                                                                    final Value seconds)
        throws Fail.Exception {
        try {
            DriverManager.setLoginTimeout(seconds.asBlock().asInt32());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_drivermanager_set_login_timeout(CodeRunner, Value)'

} // end class 'DriverManagers'
